package edu.ufop.vaadin.view;

import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

import edu.ufop.vaadin.disciplina.Disciplina;
import edu.ufop.vaadin.jdbc.dao.DisciplinaDao;

@SuppressWarnings("serial")
public class CadastrarDisciplinaView extends CustomComponent implements View,
		Button.ClickListener {
	static final String NAME = "CadatrarDisciplina";

	// String nome, String endreco, int doc, String telefone, String email,
	// String usuario, String senha, int matricula, String sala

	private final TextField codigo;
	
	private final TextField nome;

	private final TextField ementa;
	
	public CadastrarDisciplinaView() {
		ObjectProperty<String> property = new ObjectProperty<String>("");
		// TODO Auto-generated constructor stub
		setSizeFull();

		codigo = new TextField("Codigo:");
		codigo.addValidator(new StringLengthValidator("Must be 1-10 long", 1, 10,
				false));
		codigo.setValidationVisible(false);
		codigo.setBuffered(true);
		
		nome = new TextField("Nome:", property);
		nome.addValidator(new StringLengthValidator("Must be 1-10 long", 1, 10,
				false));
		nome.setValidationVisible(false); // Don't show initially
		nome.setBuffered(true);

		ementa = new TextField("Ementa:", property);
		ementa.addValidator(new StringLengthValidator("Must be 1-10 long", 1,
				10, false));
		ementa.setValidationVisible(false);
		ementa.setBuffered(true);

		VerticalLayout fields = new VerticalLayout(codigo, nome, ementa);
		fields.setSpacing(true);
		fields.setMargin(new MarginInfo(true, true, true, false));
		fields.setSizeUndefined();

		VerticalLayout viewLayout = new VerticalLayout(fields);
		viewLayout.setSizeFull();
		// viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_LEFT);
		viewLayout.setStyleName(Reindeer.LAYOUT_BLACK);
		setCompositionRoot(viewLayout);

	}
	
	Button cadastrar = new Button("Cadastrar", new Button.ClickListener() {

		@Override
		public void buttonClick(ClickEvent event) {
			
			Disciplina disciplina = new Disciplina(1, nome.getValue(), ementa.getValue());

			DisciplinaDao dao = null;

			try {
				dao = new DisciplinaDao();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			dao.adiciona(disciplina);
						
			getUI().getNavigator().navigateTo(WelcomeView.NAME);
			

		}

	});

	
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub

		
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub
		
		

		
	}

}
